// pages/good/detail.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 9,
    if_show_box: false,
    cart_num: 1,
    goods_id: 0,
    current_no: 1,
    total_no: '',
    good_list: [],
    page: 1,
    sid: '',
    page_size: 15,
    image_detail_list: [],
    detail_data: '',
    chosen_specification_index: 0,
    specification_data: {product:[{
      name: '规格1'
    },{
      name: '规格2'
    },{
      name: '规格3'
    },{
      name: '规格3'
    },]},
    show_cart_or_buying: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data(options)
    this.get_good_detail()
    this.get_goods_list()
  },
  init_data(options) {
    let sid
    if (options.scene!=undefined) {
      sid = decodeURIComponent(options.scene)
    }
    else {
      sid = options.sid
    }
    console.log(options.sid);
    let gid = this.get_gid(sid)
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      goods_id: gid
    })
  },
  get_gid(sid) {
    let a_count = 0
    let gid = ''
    for (let item of sid) {
      if (item=='a') {
        a_count += 1
      }
      if (a_count==1 & item != 'a') {
        gid += item
      }
    }
    return gid
  },
  get_good_detail() {
    let params = {
      id: this.data.goods_id,
      uid: this.data.uid,
    }
    request.post(api.goods_details, params).then(res=>{
      this.setData({
        detail_data: res
      })
      this.get_detail_image(res.content)
      this.load_acode()
    })
  },
  go_detail(e) {
    let id = e.detail.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  get_detail_image(image_str_url) {
    let image_str_url_list = image_str_url.split(',')
    image_str_url_list = this.delete_shop_str(image_str_url_list)
    this.setData({
      image_detail_list:image_str_url_list
    })
  },
  delete_shop_str(image_str_url_list) {
    for (let i=0;i<image_str_url_list.length;i++) {
      image_str_url_list[i] = image_str_url_list[i].replace('/data/','/')
    }
    return image_str_url_list
  },
  
  collecting(e) {
    let is_follow = e.currentTarget.dataset.is_follow
    let gid = e.currentTarget.dataset.id
    let if_register = App.globalData.if_register
    if (!if_register) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    else {
      let params = {
        uid: this.data.uid,
        gid
      }
      if (is_follow==0) {
        this.follow_add(params)
      }
      else {
        this.follow_delete(params)
      }
    }
  },

  get_new_goods_list() {
    let params = {
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid 
    }
    let old_list = this.data.good_list
    request.post(api.goods_more, params).then(res=>{
      if (res.length != 0) {
        let new_list = old_list.concat(res)
        this.setData({
          good_list: new_list
        }) 
      }
      else {
        this.setData({
          noMoreData: true
        });
        util.showToast('暂无更多商品了');
      }
    })
  },
  get_goods_list() {
    let params = {
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid 
    }
    request.post(api.goods_more, params).then(res=>{
      this.setData({
        good_list: res
      })
    })
  },
  swiper_no_change(e){
    this.setData({
      current_no: e.detail.current + 1
    })
  },

  show_box(e) {
    let show_cart_or_buying = e.currentTarget.dataset.show_cart_or_buying
    this.setData({
      if_show_box: true,
      show_cart_or_buying
    })
  },
  clear_mask(){
    this.setData({
      if_show_box: false
    })
  },
  hide_box(){
    this.setData({
      if_show_box: false
    })
  },
  minus() {
    this.setData({
      cart_num: --this.data.cart_num
    })
  },
  plus() {
    this.setData({
      cart_num: ++this.data.cart_num
    })
    console.log(this.data.cart_num);
  },
  confirm_cart(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false,
      })
      util.showToast("已加入购物车")
    })
  },
  go_cart(){
    console.log(1);
    wx.switchTab({
      url: '/pages/home/cart',
    })
  },
  confirm_buying(){
    // this.setData({
    //   if_show_box:false,
    // })
    let url = '/pages/order/pay?type=2' + '&id=' + this.data.goods_id
    wx.navigateTo({
      url,
    })
  },
  choose_specification(e) {
    let chosen_specification_index = e.currentTarget.dataset.index
    this.setData({
      chosen_specification_index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var noMoreData = this.data.noMoreData;
    if (!noMoreData) {
      var pageno = this.data.page;
      pageno++;
      this.setData({
        page: pageno
      });
      this.get_new_goods_list();
    }
  },
  load_acode() {
    let id = this.data.detail_data.id
    let uid = this.data.uid
    let sid = uid + 'a' + id 
    this.setData({
      sid,
    })
    // let acode_url = "https://api.gzzsu.com/pq01/wxacodeshow?sid=" + sid
    // this.setData({
    //   acode_url
    // })
  }
  , 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var sid = this.data.sid
    var title = this.data.detail_data.name;
    var share_path = '/pages/home/acode?sid=' + sid;
    console.log(share_path)
    var shareObj = {
      title: title,
      path: share_path,
      imageUrl: '',
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          Util.showToast('分享成功');
        }
      },
      fail: function () {
        Util.showToast('分享失败');
      },
    };
    return shareObj;
  }
})